package com.chx.controller;

import com.chx.dto.CategoriesDto;
import com.chx.dto.CategoryPageQueryDto;
import com.chx.entity.Categories;
import com.chx.result.PageResult;
import com.chx.result.Result;
import com.chx.service.CategoryService;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author cp-月入过万
 * @Date 2025/5/30
 * 商品类别管理接口
 */
@RestController
@RequestMapping("/admin/category")
@Api(tags = "商品类别相关接口")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 获取所有商品类别
     * @return
     */
    @ApiOperation(value = "获取所有商品类别")
    @GetMapping("/list")
    @Cacheable(cacheNames = "categoryCache",key = "'list'")
    public Result<List<Categories>> getAllCategories() {
        log.info("获取所有商品类别");
        List<Categories> categories = categoryService.getAllCategories();
        return Result.success(categories);
    }

    /**
     * 新增商品类别
     * @return
     */
    @ApiOperation(value = "新增商品类别")
    @PostMapping
    @CacheEvict(cacheNames = "categoryCache",allEntries = true)
    public Result<String> save(@RequestBody CategoriesDto categoriesDto) {
        log.info("新增商品类别");
        categoryService.save(categoriesDto);
        return Result.success();
    }
    /**
     * 修改商品类别
     * @return
     */
    @ApiOperation(value = "修改商品类别")
    @PutMapping
    @CacheEvict(cacheNames = "categoryCache",allEntries = true)
    public Result<String> update(@RequestBody CategoriesDto categoriesDto) {
        log.info("修改商品类别");
        categoryService.update(categoriesDto);
        return Result.success();
    }

    /**
     * 删除商品类别
     * @return
     */
    @ApiOperation(value = "删除商品类别")
    @DeleteMapping("/{id}")
    @CacheEvict(cacheNames = "categoryCache",allEntries = true)
    public Result<String> delete(@PathVariable Long id) {
        log.info("删除商品类别");
        categoryService.delete(id);
        return Result.success();
    }

    /**
     * 根据商品类别id获取商品类别信息
     * @param id
     * @return
     */
    @ApiOperation(value = "根据商品类别id获取商品类别信息")
    @GetMapping("/{id}")
    @Cacheable(cacheNames = "categoryCache",key = "'id_' + #id")
    public Result<Categories> getById(@PathVariable Long id) {
        log.info("根据商品类别id获取商品类别信息");
        return Result.success(categoryService.getById(id));
    }

    /**
     * 分页查询商品类别
     */
    @ApiOperation(value = "分页查询商品类别")
    @GetMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDto categoryPageQueryDto){
        log.info("分页查询商品类别");
        return Result.success(categoryService.page(categoryPageQueryDto));
    }
}
